#include<iostream>
#include<unordered_map>
#include<set>

using namespace std;

unordered_map<string, set<int>>mp;
string s = "";

int main()
{
	int a; cin >> a;
	for (int i = 1; i <= a; i++)
	{
		while (a--)
		{
			int b; cin >> b; 
			while (b--)
			{
				cin >> s;
				mp[s].insert(i);
			}
			i++;
		}
	}
	int t; cin >> t;
	while (t--)
	{
		cin >> s;
		for (auto e : mp[s])
		{
			cout << e << " ";
		}
		cout << endl;
	}

	return 0;
}
